package com.ruoyi.web.xxj.contoller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.xxj.domain.pojo.OrderReq;
import com.ruoyi.web.xxj.domain.vo.OrderVo;
import com.ruoyi.web.xxj.enums.PayStatus;
import com.ruoyi.web.xxj.service.IClassOrderService;
import com.ruoyi.web.xxj.service.IOrderService;
import com.ruoyi.web.xxj.service.IProductOrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "订单处理Controller")
@RestController
@RequestMapping("/xxj/order")
public class OrderController extends BaseController {

    @Autowired
    private IOrderService orderService;
    @Autowired
    private IClassOrderService classOrderService;
    @Autowired
    private IProductOrderService productOrderService;

    /**
     * 审核取消课程订单
     */
    @ApiOperation("审核取消课程订单")
    @GetMapping("/auditCancle")
    public AjaxResult auditCancle(@RequestParam @ApiParam(required = true, value = "类型：class/product") String type,
                                  @RequestParam @ApiParam(required = true, value = "课程订单主键ID") Long id,
                                  @RequestParam @ApiParam(required = true, value = "审核状态：3-审核退款，5-拒绝退款") String auditStatus,
                                  @RequestParam(required = false) @ApiParam(value = "拒绝退款原因") String rejectReason) {
        return AjaxResult.success(orderService.auditCancle(type, id, auditStatus, rejectReason));
    }

    /******************************************* 以下是小程序接口 ******************************************************/

    /**
     * 查询订单列表
     */
    @ApiOperation("[小程序]查询个人订单列表")
    @GetMapping("/miniList")
    public TableDataInfo<OrderVo> miniList(@ApiParam(value = "支付状态：0-未支付；1-已支付；2-退款待审核；3-退款通过审核；4-拒绝退款；5-已退款；6-退款失败；7-取消订单")
                               @RequestParam(required = false) String payStatus) {
        if (null == SecurityUtils.getLoginUser() || null == SecurityUtils.getLoginUser().getWechatUser()) {
            throw new ServiceException("请登录后再查询订单信息！");
        }
        Long wechatUserId = SecurityUtils.getLoginUser().getWechatUser().getId();
        startPage();
        List<OrderVo> list = orderService.selectOrderVos(new OrderReq(wechatUserId, payStatus));
        return getDataTable(list);
    }
    
    @ApiOperation("[小程序]查看订单详情")
    @GetMapping("/orderDetail")
    public AjaxResult getOrderDetail(@RequestParam @ApiParam(required = true, value = "类型：class/product") String type, 
    								 @RequestParam @ApiParam(required = true, value = "订单ID") Long orderId) {
    	
    	Long wechatUserId = SecurityUtils.getLoginUser().getWechatUser().getId();
    	if (type.equals("class")) {
			return success(orderService.selectClassOrderVo(new OrderReq(wechatUserId,orderId)));
		}else if (type.equals("product")) {
			return success(orderService.selectProductOrderVo(new OrderReq(wechatUserId,orderId)));
		}else {
			throw new ServiceException("类型有误，必须是class或者product！");
		}
    }

    /**
     * 撤销取消课程订单
     */
    @ApiOperation("[小程序]撤销取消课程/文创订单")
    @GetMapping("/withdraw")
    public AjaxResult withdraw(@RequestParam @ApiParam(required = true, value = "类型：class/product") String type,
                               @RequestParam @ApiParam(required = true, value = "课程/文创订单主键ID") Long id) {
        if (StringUtils.isEmpty(type) || (!type.equals("class") && !type.equals("product"))) {
            throw new ServiceException("审核类型有误，必须是class或者product！");
        }
        if (type.equals("class")) {
            return toAjax(classOrderService.withdraw(id));
        } else {
            return toAjax(productOrderService.withdraw(id));
        }

    }

    /**
     * 取消课程订单
     */
    @ApiOperation("[小程序]取消课程/文创订单")
    @GetMapping("/cancel")
    public AjaxResult cancel(@RequestParam @ApiParam(required = true, value = "类型：class/product") String type,
                             @RequestParam @ApiParam(required = true, value = "课程订单主键ID") Long id,
                             @RequestParam(required = false) @ApiParam(value = "取消订单原因") String cancelReason) {
        if (StringUtils.isEmpty(type) || (!type.equals("class") && !type.equals("product"))) {
            throw new ServiceException("审核类型有误，必须是class或者product！");
        }
        if (type.equals("class")) {
            return toAjax(classOrderService.cancel(id, cancelReason));
        } else {
            return toAjax(productOrderService.cancel(id, cancelReason));
        }
    }
    
    @ApiOperation("[小程序]获取退款订单信息")
    @GetMapping("/refundList")
    public TableDataInfo<OrderVo> refundList() {
    	if (null == SecurityUtils.getLoginUser() || null == SecurityUtils.getLoginUser().getWechatUser()) {
            throw new ServiceException("请登录后再查询订单信息！");
        }
        Long wechatUserId = SecurityUtils.getLoginUser().getWechatUser().getId();
        List<String> payStatusList = new ArrayList<String>();
        payStatusList.add(PayStatus.REFUND_TO_AUDIT.getValue());
        payStatusList.add(PayStatus.REFUND_PASS_AUDIT.getValue());
        payStatusList.add(PayStatus.REFUND_REJECT.getValue());
        payStatusList.add(PayStatus.REFUNDED.getValue());
        payStatusList.add(PayStatus.REFUND_FAIL.getValue());
        OrderReq req = new OrderReq(wechatUserId,payStatusList);
        startPage();
        List<OrderVo> list = orderService.selectOrderVos(req);
		return getDataTable(list);
    }
}
